﻿$(function () {
    bindData(1);
});


/*
yaols 20171019
分页数据
*/
function bindData(current) {

    var tbody = $("#tbody1");
    $(tbody).empty();
    var title = $(" <tr class='odd_bg'>"
        + "<th align='center' align='left' width='12%'>昵称</th>"
        + "<th align='center' align='left' width='12%'>头像</th>"
        + "<th align='center' align='left' width='12%'>登录名</th>"
        + "<th align='center' align='left'>手机</th>"
        + "<th align='center' align='left'>角色</th>"
        + "<th align='center' width='12%'>邮箱地址</th>"
        + "<th align='center' width='8%'>状态</th>"
        + "<th align='center' >创建时间</th>"
        + "<th align='center' width='12%'>操作</th>"
        + "</tr>");
    $(tbody).append(title);
    $("#page1").hide();
    var json = {};
    json.Phone = $("#Phone").val();
    json.CurrentIndex = current || 1;
    $.getJSON('/ManagerUser/LoadData', json, function (res) {
        //此处仅仅是为了演示变化的内容
        if (res.List != null && res.List.length > 0) {
            $("#total").text("总数：" + res.Total);
            for (var i = 0; i < res.List.length; i++) {
                var item = res.List[i];
                var tr = $("<tr>"
                    + "<td align='center'>" + item.real_name + "</td>"
                    + "<td align='center'><img src='" + item.headimg + "' height='50' width='50' /></td>"
                    + "<td align='center'>" + item.user_name + "</td>"
                    + "<td align='center'>" + item.telephone + "</td>"
                    + "<td align='center'>" + item.RoleName + "</td>"
                    + "<td align='center'>" + item.email + "</td>"
                    + "<td align='center'>" + (item.IsEnable == 0 ? "正常" : "禁用") + "</td>"
                    + "<td align='center'>" + formatDate(item.add_time) + "</td>"
                    + "<td align='center'>"
                                + (item.IsEnable == 0 ? "<a data-operate='disable' href='javascript:;' data-id=" + item.id + ">禁用</a>" : "<a data-operate='enable' href='javascript:;' data-id=" + item.id + ">启用</a>")
                               + "|"
                                //+ "<a data-operate='change' href='javascript:;' data-id=" + item.UserCode + ">重置密码</a>"
                                //+ "|"
                                + "<a  href='/ManagerUser/Edit?id=" + item.id + "'>编辑</a>"
                            + "</td>"
                    + "</tr>");
                $(tbody).append(tr);
            }
            $("#page1").show();
            //显示分页
            laypage({
                cont: 'page1', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
                pages: res.Count, //通过后台拿到的总页数
                curr: current || 1, //当前页
                skip: true,
                jump: function (obj, first) { //触发分页后的回调
                    if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
                        bindLogs(obj.curr);
                    }
                }
            });
        } else {
            $("#tbody1").append("<tr><td colspan='8'>未查询到数据</td></tr>");
            $("#page1").hide();
        }
    }, function (error) {
        alert(error);
    });

}


//禁用
$('#tbody1').delegate('td a[data-operate=disable]', 'click', function () {
    _this = this;
    var id = $(this).attr("data-id");
    var deldialog = parent.dialog({
        title: "提示信息",
        content: '确定禁用？',
        ok: function () {
            $.ajax({
                type: "POST",
                url: "/ManagerUser/IsEnable",
                data: { id: id, enable: 1 },
                dataType: "json",
                success: function (data) {
                    if (data.Success) {
                        window.location.reload();
                    } else {
                        alert("禁用失败");
                    }
                }
            });
        },
        okValue: '确定',
        cancelValue: '取消',
        cancel: function () {
        }
    }).showModal();
});


//启用
$('#tbody1').delegate('td a[data-operate=enable]', 'click', function () {
    _this = this;
    var id = $(this).attr("data-id");
    var deldialog = parent.dialog({
        title: "提示信息",
        content: '确定要启用吗？',
        ok: function () {
            $.ajax({
                type: "POST",
                url: "/ManagerUser/IsEnable",
                data: { id: id, enable: 0 },
                dataType: "json",
                success: function (data) {
                    if (data.Success) {
                        window.location.reload();
                    } else {
                        alert("启用失败");
                    }
                }
            });
        },
        okValue: '确定',
        cancelValue: '取消',
        cancel: function () {
        }
    }).showModal();
});